<?php
/**
 * @author Administrator
 * @date 2022/11/18 11:00
 * @desciption:
 */

namespace JsonApi;


class Fields
{
    public static $f    = [];
    public static $bind = [];

    public static function getAll(){
        return self::$f;
    }

    public static function get(string $key){
        return isset(self::$f[$key]) ? [$key => $f[$key]] : null;
    }

    public static function set(array $fields){
        self::$f = array_merge(self::$f,$fields);
    }

    /**
     * 初始化字段
     * 将绑定的字段挂载到f中去
     */
    public static function init(){
        if(!empty(self::$bind)){
            foreach(self::$bind AS $name => $value){
                if(is_callable($value)){
                    self::$f[$name] = $value();
                }elseif (is_array($value) && method_exists($value[0],$value[1])){
                    if(class_exists($value[0])){
                        if(!method_exists($value[0],$value[1])){
                            $value[0] = new $value[0];
                        }
                        if(!method_exists($value[0],$value[1])){
                            self::$f[$name] = call_user_func_array($value);
                        }
                    }
                }elseif (class_exists($value)){
                    self::$f[$name] = new $value;
                }
            }
        }
    }

}